// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

//Shader written by Alex Dixon
Shader "Custom/Spine/SkeletonGhost" 
{
    Properties 
    {
        _Color ("Main Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
        _AlphaTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
		_TextureFade ("Texture Fade Out", Range(0,1)) = 0
    }
    SubShader 
    {
    
      Tags {"Queue"="Transparent" "IgnoreProjector"="False" "RenderType"="Transparent"}
      Fog { Mode Off }
      Blend One OneMinusSrcAlpha
      ZWrite Off
	  Cull Off
      
        Pass 
        {
            Tags {"LightMode" = "Always"}                      // This Pass tag is important or Unity may not give it the correct light information.
           		CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                //#pragma multi_compile_fwdbase                       // This line tells Unity to compile this pass for forward base.
                
                #include "UnityCG.cginc"
                //#include "AutoLight.cginc"
                    
               	struct vertex_input
               	{
               		float4 vertex : POSITION;
               		float2 texcoord : TEXCOORD0;
					float4 color : COLOR;
               	};
                
                struct vertex_output
                {
                    float4 pos         : SV_POSITION;
                    float2 uv          : TEXCOORD0;
                    fixed gray         : TEXCOORD1;
					float4 color       : COLOR;
                };
                
                sampler2D _MainTex;
                sampler2D _AlphaTex;
                fixed4 _Color;
				fixed _TextureFade;
                
                vertex_output vert (vertex_input v)
                {
                    vertex_output o;
                    o.pos = UnityObjectToClipPos( v.vertex);
                    o.uv = v.texcoord.xy;
                    o.color = v.color * _Color;
					o.gray = dot(v.color, fixed4(1, 1, 1, 0));
                    return o;
                }
                
                fixed4 frag(vertex_output i) : COLOR
                {
                    fixed4 tex = tex2D(_MainTex, i.uv);
                    fixed3 alpha = tex2D(_AlphaTex, i.uv);
                    
					fixed4 ret = fixed4(max(_TextureFade, tex.r), max(_TextureFade, tex.g), max(_TextureFade, tex.b), alpha.r);
                    
                    // 灰态
                    if (i.gray == 0 && i.color.a >= 0.01f)
                    {
                        fixed grey = dot(ret.rgb, float3(0.299, 0.587, 0.114));
                        ret.rgb = fixed3(grey, grey, grey);
                        ret.a *= i.color.a;
                    }
                    else
                    {
                        ret *= i.color ;
                    }
                    
					return ret;



					//float finalAlpha = tex.a * i.color.a * _Color.a;

                    /*
                    TODO:  Add basic lighting stuff in later?

                    fixed4 c;
					c.rgb = (UNITY_LIGHTMODEL_AMBIENT.rgb * tex.rgb);       // Ambient term. Only do this in Forward Base. It only needs calculating once.
                    c.rgb += tex.rgb; // Diffuse and specular.
					//Unity 4: c.rgb = (UNITY_LIGHTMODEL_AMBIENT.rgb * tex.rgb * 2);       // Ambient term. Only do this in Forward Base. It only needs calculating once.
					//Unity 4: c.rgb += (tex.rgb * _LightColor0.rgb * diff) * (atten * 2); // Diffuse and specular.
                    c.a = tex.a;  // + _LightColor0.a * atten;

                    return c;
					*/
                }
            ENDCG
        }
             
        
    }
    //FallBack "Transparent/Cutout/VertexLit"    // Use VertexLit's shadow caster/receiver passes.
}